package com.wakyde.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        Person person = new Student();
        System.out.println("这个人是:"+person.name);

        // 方式1: 通过对象获得
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());

        // 方式2: forname获得
        Class c2 = Class.forName("com.wakyde.reflection.Student");
        System.out.println(c2.hashCode());

        // 方式3: 通过类名.class获得
        Class c3 = Student.class;
        System.out.println(c3.hashCode());

        // 方式4: 基本内置类型的包装类都有一个Type属性
        Class c4 = Integer.TYPE;
        System.out.println(c4);

        // 获得父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);

        // 获取当前Class对象的接口
        Class[] c6 = c1.getInterfaces();
        System.out.println(c6);

        // 返回该类的类加载器
        ClassLoader c7 = c1.getClassLoader();
        System.out.println(c7);

        // 返回一个包含某些Constructor对象的数组
        Constructor c8 = c1.getConstructor();
        System.out.println(c8);

        // 返回一个Method对象,此对象的形参类型为paramType
        Method c9 = c1.getMethod("test");
        System.out.println(c9);

        // 返回一个Field对象的一个数组
        Field[] c10 = c1.getDeclaredFields();
        System.out.println(c10);
        
    }
}


class Person{
    String name;

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}


class Student extends Person{
    public Student(){
        this.name = "学生";
    }

    public void test(){

    }
}


class Teacher extends Person{
    public Teacher(){
        this.name = "老师";
    }
}
